package org.ypx.web.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.ypx.web.App;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class SessionInterceptor implements HandlerInterceptor {

	private Logger logger = LoggerFactory.getLogger(getClass());
	private String TAG = SessionInterceptor.class.getSimpleName();

	@Override
	public boolean preHandle(HttpServletRequest request,
							 HttpServletResponse response, Object handler) throws Exception {
		logger.info(TAG + " preHandle");
		String uri = request.getRequestURI();
		logger.info("uri={}", uri);
		
		uri = uri.replaceFirst(request.getContextPath(), "");

		if (! uri.startsWith("/app/page/")) {
			return true;
		}
		logger.info(TAG + " preHandle :"+request.getSession().getAttribute(App.USER_SESSION_KEY));
		if (request.getSession().getAttribute(App.USER_SESSION_KEY) == null) {
			// 未登录
			PrintWriter out = response.getWriter();
			StringBuilder builder = new StringBuilder();
			builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
			builder.append("window.top.location.href=\"");
			builder.append(request.getContextPath());
			builder.append("/\";</script>");
			out.print(builder.toString());
			out.close();
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void postHandle(HttpServletRequest request,
						   HttpServletResponse response, Object handler,
						   ModelAndView modelAndView) throws Exception {
		logger.info(TAG+" postHandle");
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
								HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		logger.info(TAG+" afterCompletion");
	}

}
